//
//  ZStatusOriginalView.m
//  OC-weibo
//
//  Created by zero on 17/3/24.
//  Copyright © 2017年 zero. All rights reserved.
//

#import "ZStatusOriginalView.h"
#import "ZCollectionView.h"

@interface ZStatusOriginalView()

//  头像
@property (nonatomic,weak)UIImageView *imgIcon;

//  name
@property (nonatomic,weak)UILabel *labName;

//  vip
@property (nonatomic,weak)UIImageView *imgMbrank;

//  time
@property (nonatomic,weak)UILabel *labTime;

//  source
@property (nonatomic,weak)UILabel *labSource;

//  认证用户类型
@property (nonatomic,weak)UIImageView *imgVerified;

//  正文
@property (nonatomic,weak)UILabel *labContent;

@property (nonatomic,weak)ZCollectionView *collectionView;

//  记录约束
@property (nonatomic,strong)MASConstraint *constraint;

@end

@implementation ZStatusOriginalView

#pragma mark
#pragma mark - 重写init方法
- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        //  布局
        [self setupUI];
    }
    
    return self;
}

#pragma mark
#pragma mark - 布局
- (void)setupUI{
    
    //  头像
    UIImageView *imgIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_default"]];
    
    self.imgIcon = imgIcon;
    
    //  添加
    [self addSubview:imgIcon];
    
    //  约束
    [imgIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.top.equalTo(self).offset(8);
        make.size.mas_equalTo(CGSizeMake(35, 35));
        
    }];
    
    //  姓名
    UILabel *labName = [UILabel z_labelWithText:@"阿拉就叫狗蛋" andFontSize:14 andColor:[UIColor blackColor]];
    
    self.labName = labName;
    
    [self addSubview:labName];
    
    //  约束
    [labName mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(imgIcon.mas_trailing).offset(8);
        make.top.equalTo(imgIcon);
    }];
    
    //  微博等级
    UIImageView *imgMbrank = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_membership_expired"]];
    
    self.imgMbrank = imgMbrank;
    
    [self addSubview:imgMbrank];
    
    //  约束
    [imgMbrank mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(labName);
        make.leading.equalTo(labName.mas_trailing).offset(8);
    }];
    
    //  时间
    UILabel *labTime = [UILabel z_labelWithText:@"微博时间" andFontSize:14 andColor:[UIColor orangeColor]];
    
    self.labTime = labTime;
    
    [self addSubview:labTime];
    
    //  约束
    [labTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(imgIcon);
        make.leading.equalTo(labName);
    }];
    
    //  来源
    UILabel *labSource = [UILabel z_labelWithText:@"微博来源" andFontSize:14 andColor:[UIColor darkGrayColor]];
    
    self.labSource = labSource;
    
    [self addSubview:labSource];
    
    //  约束
    [labSource mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(labTime);
        make.leading.equalTo(labTime.mas_trailing).offset(8);
    }];
    
    //  认证
    UIImageView *imgVerified = [[UIImageView alloc] init];
    
    self.imgVerified = imgVerified;
    [self addSubview:imgVerified];
    
    //  约束
    [imgVerified mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(imgIcon.mas_trailing);
        make.centerY.equalTo(imgIcon.mas_bottom);
    }];
    
    //  正文
    UILabel *labContent = [UILabel z_labelWithText:nil andFontSize:14 andColor:[UIColor blackColor]];
    
    self.labContent = labContent;
    
    labContent.preferredMaxLayoutWidth = 355;
    labContent.numberOfLines = 0;
    
    [self addSubview:labContent];
    
    //  约束
    [labContent mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(imgIcon.mas_bottom).offset(8);
        make.leading.equalTo(imgIcon);
        
    }];
    

    //  添加collection
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    ZCollectionView *collectionView = [[ZCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];;
    
    
    self.collectionView = collectionView;
    
    [self addSubview:collectionView];
    
    //  添加约束
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(labContent.mas_bottom).offset(8);
        make.leading.offset(10);
        
    }];
    
    
    //  自动约束
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        
        self.constraint = make.bottom.equalTo(collectionView.mas_bottom);
    }];
    
}

#pragma mark
#pragma mark - 重写Models的方法
- (void)setHomeViewModel:(ZHomeModelViewModel *)homeViewModel{
    
    _homeViewModel = homeViewModel;
    
    self.labName.text = homeViewModel.userModel.user.name;
    self.imgMbrank.image = homeViewModel.imgMbrank;
    self.imgVerified.image = homeViewModel.imgVerified;
    [self.imgIcon sd_setImageWithURL:homeViewModel.urlProfilr];
    
    //  原创微博内容
    self.labContent.text = homeViewModel.userModel.text;
    
    //   时间
    self.labTime.text = homeViewModel.timeString;
 
    //  来源
    self.labSource.text = homeViewModel.soucreString;
    

    NSInteger count = homeViewModel.userModel.pic_urls.count;
    
    [self.constraint deactivate];

    if (count == 0) {
        
        self.collectionView.hidden = YES;
        
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.equalTo(self.labContent.mas_bottom);
        }];
        

        
    }else{
        
        
        self.collectionView.hidden = NO;
        //  图片
        self.collectionView.picModel = homeViewModel.userModel.pic_urls;
        
        
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.equalTo(self.collectionView.mas_bottom);
            
        }];
        
    }
 
 
}
@end
